package com.watson.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.watson.R;

import java.util.ArrayList;
import java.util.Map;


public class Module2Adapter extends RecyclerView.Adapter<Module2Adapter.ItemViewHolder> {
    private ItemClickListener mItemClick;
    private Context mContext;
    private ArrayList<Map> mList;
    private ArrayList<Map> cornermark = new ArrayList<>();

    public Module2Adapter(Context mContext, ArrayList<Map> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setData(ArrayList<Map> vList) {
        this.mList = vList;
        notifyDataSetChanged();
    }

    public void cornermark(ArrayList<Map> cornermark) {
        this.cornermark = cornermark;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater
                .from(mContext).inflate(R.layout.itme_module2, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        if (mList.size() > 0) {
            holder.tvName.setText(mList.get(position).get("R_Fun_Name") + "");
            String F_ImgURL = mList.get(position).get("F_ImgURL").toString();
            if (!F_ImgURL.startsWith("http")) {
                F_ImgURL = "http://wmsapi.vocen.com.cn:9091/" + F_ImgURL;
            }
            Glide.with(mContext).load(F_ImgURL).error(R.mipmap.icon_renwu).into(holder.imgView);

            holder.tvChuNum.setVisibility(View.INVISIBLE);
            if (cornermark.size() > 0) {
                for (int i = 0; i < cornermark.size(); i++) {
                    int number = Integer.valueOf(cornermark.get(i).get("number") + "");
                    String iv = cornermark.get(i).get("Feature_ID") + "";
                    if (iv.equals(mList.get(position).get("R_Fun_Id") + "")) {
                        if (number > 0) {
                            holder.tvChuNum.setVisibility(View.VISIBLE);
                            holder.tvChuNum.setText(number + "");
                        }
                        if (number > 99) {
                            holder.tvChuNum.setText("99+");
                        }
                    }
                }
            }

            holder.itemView.setOnClickListener(v -> {
                if (mItemClick != null) {
                    mItemClick.onClick(position, mList.get(position));
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvChuNum, tvName;
        private ImageView imgView;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            imgView = itemView.findViewById(R.id.imgView);
            tvChuNum = itemView.findViewById(R.id.tvChuNum);
        }
    }

    public interface ItemClickListener {
        void onClick(int position, Map mList);
    }
}
